﻿using PSI.Helper;
using PSI.Services;
using PSI.Services.TMS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PSI.Models.TMS;
using System.Collections.Specialized;
using Newtonsoft.Json;
using System.Web.Script.Serialization;

namespace PSI.Controllers {
    /// <summary>
    /// 进口委托
    /// </summary>
    public class InConsignationController : PSIController {

        public ActionResult Index() {
            var ps = new PermissionService();

            if (!ps.HasPermission(FIdConst.IN_CONSIGNATION)) {
                return Redirect("/");
            }

            ViewBag.UserName = UserService.GetLoginUserName();

            ViewBag.PermissionEdit = ps.HasPermission("3001-1") ? "1" : "0";
            ViewBag.PermissionConfirm = ps.HasPermission("3001-2") ? "1" : "0";

            return View();
        }

        public ActionResult MakePDF(string id)
        {
            var model = new InConsignationService().GetInConsignationDetail(id);
            try
            {
                new PSI.Helper.MakePDF().CreatImprotPDF(model);
                return new EmptyResult();
            }
            catch (Exception ex)
            {
                return new EmptyResult();
            }
            
        }

        [HttpPost]
        public ActionResult GetInConsignationListData() {
            var data = new InConsignationService().GetInConsignationListData(this.Request.Form);
            return Json(data);
        }

        [HttpPost]
        public ActionResult GetInConsignationDetailListData(string id) {
            var data = new InConsignationService().GetInConsignationDetailListData(id);
            return Json(data);
        }

        [HttpPost]
        public ActionResult EditInConsignation() {
            var result = new InConsignationService().EditInConsignation(this.Request.Form);

            return Json(result);
        }

        [HttpPost]
        public ActionResult DeleteInConsignation(string id) {
            var result = new InConsignationService().DeleteInConsignation(id);

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetInConsignation(string id) {
            var data = new InConsignationService().GetInConsignation(id);

            return Json(data);
        }

        [HttpPost]
        public ActionResult EditInConsignationDetail() {
            var result = new InConsignationService().EditInConsignationDetail(this.Request.Form);

            return Json(result);
        }

        [HttpPost]
        public ActionResult CheckCntNumber(string cntNumber1, string cntNumber2) {
            var result = new InConsignationService().CheckCntNumber(cntNumber1, cntNumber2);

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetInConsignationDetail(string id) {
            var data = new InConsignationService().GetInConsignationDetail(id);

            return Json(data);
        }

        [HttpPost]
        public ActionResult DeleteInConsignationDetail(string id, string parentId) {
            var result = new InConsignationService().DeleteInConsignationDetail(id, parentId);

            return Json(result);
        }

        [HttpPost]
        public ActionResult MakePlan(string id, string actionDate, string isweixin, string dispatcher)
        {
            var result = new InConsignationService().MakePlan(id, actionDate, isweixin, dispatcher);




            return Json(result);
        }

        [HttpPost]
        public ActionResult CancelPlan(string id) {
            var result = new InConsignationService().CancelPlan(id);

            return Json(result);
        }

        [HttpPost]
        public ActionResult Upload() {
            try {
                var file = this.Request.Files["file"];
                if (!file.FileName.EndsWith(".xls")) {
                    return Content("{success: false, msg: '请选择Excel文件'}");
                }

                var path = this.Server.MapPath("~");
                path += @"upload";
                if (!Directory.Exists(path)) {
                    Directory.CreateDirectory(path);
                }

                var fileName = path + @"\" + IdGeneratorService.NewId() + "_" + file.FileName;

             
                file.SaveAs(fileName);
              //  var Newfile=new InConsignationService().ConventWPS(fileName);


                var result = new InConsignationService().Upload(fileName);

                return Content(result);
            } catch (Exception ex) {
                return Content("{success: false, msg: 'Execl版本过低，请用最新版本打开保存一下文件再上传'}");
            }
        }


        public ActionResult ReserationImportView()
        {
            ViewBag.UserName = UserService.GetLoginUserName();
            return View();
        }

        [HttpPost]
        public ActionResult ImportResViewQuery()
        {
            var cc = (NameValueCollection)this.Request.Params;

            var st = cc["sdt"];
            var result = new InConsignationService().GetInConsignationDetailListData("").Where(a => a.isweixin == IsWXStatus.release || a.isweixin == IsWXStatus.complete);
            if (!string.IsNullOrEmpty(cc["adr"].ToString()))
            {
                result = result.Where(a => a.Address == cc["adr"]);
            }

            var dt = cc["date"];
            if (dt.Contains("今天")){
                result = result.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd"));
            }
            else if(dt.Contains("明天")) {
                result = result.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"));
            } else if (dt.Contains("昨天")) {
                result = result.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"));
            }
            var tempobj = new { Data = result.ToList<InConsignationDetail>() };

            return Json(tempobj);
        }

        public ActionResult GetimportRes(string date)
        {
            var cc = new InConsignationService().GetInConsignationDetailListData("").Where(a => a.isweixin == IsWXStatus.release || a.isweixin == IsWXStatus.complete).ToList<InConsignationDetail>();

            if(date.Contains("今天"))
            {
                cc = cc.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd")).ToList<InConsignationDetail>();
            } else if (date.Contains("明天"))
            {
                cc = cc.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")).ToList<InConsignationDetail>();
            }
            else if(date.Contains("昨天"))
            {
                cc = cc.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")).ToList<InConsignationDetail>();
            }
                     
            var data = new { Data = cc };

            return Json(data);
        }

        [HttpPost]
        public ActionResult GetResListbyOrderID(string orderID)
        {
            var result = new WinXinResServer().GetimportByorder(orderID);
            var cc = new { data = result };
            return Json(cc);
        }

        [HttpPost]
        public ActionResult ResDecision(string id)
        {
            var inmportdemil =new WinXinResServer().GetSinglebyID(id,1);

            var importdateil = new InConsignationService().GetInConsignationDetail(inmportdemil.orderID.ToString());
         
            if (importdateil.Status!="待派车"&& importdateil.Status!="待下达计划") return Json("当前计划已经被安排车辆！");
            
            var bol = new WinXinResServer().ImportResDecision(id);
            if (bol)
                return Json("");
            else
                return Json("数据问题，审核通过失败！");
        }

        public ActionResult ReportDetile()
        {
            ViewBag.UserName = UserService.GetLoginUserName();

           // var data = new InPlanService().GetInPlanListData(this.Request.QueryString);

            var time=Request.QueryString["time"].ToString();
            var add=Request.QueryString["address"].ToString();

            if (time == "本月小计")
            {
                time = DateTime.Now.ToString("yyyy-MM");              
            }
            if (add == "本月合计")
            {
                add = "";
            }

            ViewBag.time=time;
            ViewBag.address = add;

            return View();
        }
    }
}
